package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：lintao
 * @date ：Created in 2020/12/1
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    // 获取所有的套餐信息
    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        Jedis jedis = jedisPool.getResource();
        try {
            // 从redis中查询，如果没有，去查询数据库，并保存redis
            String setmeals = jedis.get("setmeals");
            if (setmeals == null || setmeals.length() == 0){
                List<Setmeal> setmealList = setmealService.findAll();
                String json = JSON.toJSONString(setmealList);
                jedis.set("setmeals",json);
                return new Result(true,MessageConstant.GET_SETMEAL_LIST_SUCCESS,setmealList);
            }
            return new Result(true,MessageConstant.GET_SETMEAL_LIST_SUCCESS,JSON.parse(setmeals));
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }finally {
            jedis.close();
        }
    }

    // 根据id获取套餐详细信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Jedis jedis = jedisPool.getResource();
        Setmeal setmeal = null;
        try {
            String setmeals = jedis.get("setmeals");
            if (setmeals == null || setmeals.length() == 0){
                setmeal = setmealService.findById(id);
                //return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
            }
            List list = (List) JSON.parse(setmeals);
            List<Setmeal> setmealList = new ArrayList<>();
            for (Object o : list) {
                String s = JSON.toJSONString(o);
                Setmeal sm = JSON.parseObject(s, Setmeal.class);
                setmealList.add(sm);
            }
            for (Setmeal sm : setmealList) {
                if (sm.getId() == id){
                    //return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
                    setmeal = sm;
                }
            }
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }finally {
            jedis.close();
        }
    }
}
